package com.boruan.qq.zxgylibrary.ui.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.LogUtils;
import com.boruan.qq.zxgylibrary.R;
import com.boruan.qq.zxgylibrary.R2;
import com.boruan.qq.zxgylibrary.base.BaseOneActivity;
import com.boruan.qq.zxgylibrary.service.model.AddressBackPriceBean;
import com.boruan.qq.zxgylibrary.service.model.AddressListBean;
import com.boruan.qq.zxgylibrary.service.presenter.AddressPresenter;
import com.boruan.qq.zxgylibrary.service.view.AddressView;
import com.boruan.qq.zxgylibrary.ui.adapters.AddressAdapter;
import com.boruan.qq.zxgylibrary.utils.CustomDialogOne;
import com.boruan.qq.zxgylibrary.utils.RecycleViewDivider;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by 崔琦 on 2019/8/12.
 * des: ....
 */

public class AddressListActivity extends BaseOneActivity implements AddressView{
    @BindView(R2.id.rv_address)
    RecyclerView rvAddress;
    @BindView(R2.id.ll_cant_delivery)
    LinearLayout llCantDelivery;
    @BindView(R2.id.ll_add_view)
    LinearLayout llAddView;

    private AddressAdapter addressAdapter;
    public static final int ADD_NEW_ADDRESS_CODE = 10;
    private AddressPresenter addressPresenter;
    private CustomDialogOne customDialogOne;
    /** 商铺Id */
    private String shopId;
    private List<AddressListBean.ResultBean> mCanDeliveryList;
    private List<AddressListBean.ResultBean> mCanNotDeliveryList;

    @Override
    protected int getLayoutId() {
        return R.layout.drug_activity_address_list;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        if (getIntent() != null){
            shopId = getIntent().getStringExtra("shopId");
        }
        mCanDeliveryList = new ArrayList<>();
        mCanNotDeliveryList = new ArrayList<>();
        customDialogOne = new CustomDialogOne(this,R.style.CustomDialogOne);
        addressPresenter = new AddressPresenter(this);
        addressPresenter.onCreate();
        addressPresenter.attachView(this);

        LinearLayoutManager manager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false){
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        rvAddress.setLayoutManager(manager);
        addressAdapter = new AddressAdapter(this,shopId,addressPresenter);
        rvAddress.setAdapter(addressAdapter);
        rvAddress.addItemDecoration(new RecycleViewDivider(
                this, LinearLayoutManager.HORIZONTAL, 2, getResources().getColor(R.color.divide)));

    }

    @Override
    protected void onResume() {
        super.onResume();
        addressPresenter.getAddressListData(shopId);
    }

    @OnClick({R2.id.iv_back, R2.id.tv_add_address})
    public void onViewClicked(View view) {
        int i = view.getId();
        if (i == R.id.iv_back) {
            if(mCanDeliveryList == null || mCanDeliveryList.size() == 0){
                Intent intent = new Intent();
                intent.putExtra("addressId",-1);
                setResult(13,intent);
            }
            finish();
        } else if (i == R.id.tv_add_address) {
            Intent intent = new Intent(AddressListActivity.this, AddAddressActivity.class);
            intent.putExtra("type", "add");
            startActivityForResult(intent, ADD_NEW_ADDRESS_CODE);

        }
    }

    @Override
    public void onBackPressed() {
        if(mCanDeliveryList == null || mCanDeliveryList.size() == 0){
            LogUtils.e("onBackPressed111"+mCanDeliveryList.size());
            Intent intent = new Intent();
            intent.putExtra("addressId",-1);
            setResult(13,intent);
        }
        finish();
        super.onBackPressed();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == ADD_NEW_ADDRESS_CODE && resultCode == 11){
            //新增地址刷新
            addressPresenter.getAddressListData(shopId);
        }else if (requestCode == ADD_NEW_ADDRESS_CODE && resultCode == 12){
            //删除地址刷新
            addressPresenter.getAddressListData(shopId);
        }
    }

    @Override
    public void showProgress() {
        customDialogOne.show();
    }

    @Override
    public void hideProgress() {
        customDialogOne.dismiss();
    }

    @Override
    public void addAddressSuccess(String msg) {

    }

    @Override
    public void addAddressFailed(String msg) {

    }

    @Override
    public void deleteAddressSuccess(String msg) {

    }

    @Override
    public void deleteAddressFailed(String msg) {

    }

    @Override
    public void modifyAddressSuccess(String msg) {

    }

    @Override
    public void modifyAddressFailed(String msg) {

    }

    @Override
    public void getAddressListSuccess(List<AddressListBean.ResultBean> addressListBean) {
        mCanDeliveryList.clear();
        mCanNotDeliveryList.clear();
        for (int i = 0 ; i < addressListBean.size(); i++){
            if (addressListBean.get(i).getCanDelivery() == 1){
                mCanDeliveryList.add(addressListBean.get(i));
            }else {
                mCanNotDeliveryList.add(addressListBean.get(i));
            }
        }
        addressAdapter.setData(mCanDeliveryList);

        if (mCanNotDeliveryList.size() > 0){
            llCantDelivery.setVisibility(View.VISIBLE);
            llAddView.removeAllViews();
            for (int j = 0; j < mCanNotDeliveryList.size();j++){
                View view = LayoutInflater.from(AddressListActivity.this).inflate(R.layout.drug_item_address, null);
                TextView tvUserAddressName = (TextView)view.findViewById(R.id.tv_user_address_name);
                TextView tvUserNamePhone = (TextView)view.findViewById(R.id.tv_user_name_phone);
                tvUserAddressName.setText(mCanNotDeliveryList.get(j).getFulladdress());
                String mSex = "";
                if (mCanNotDeliveryList.get(j).getSex() == 1){
                    mSex = "先生";
                }else if (mCanNotDeliveryList.get(j).getSex() == 2){
                    mSex = "女士";
                }
                tvUserNamePhone.setText(mCanNotDeliveryList.get(j).getContactperson() + "(" + mSex + ")  " + mCanNotDeliveryList.get(j).getContactphone());
                llAddView.addView(view);
            }
        }else {
            llCantDelivery.setVisibility(View.GONE);
        }
    }

    @Override
    public void getAddressListFailed(String msg) {
        if(TextUtils.isEmpty(msg)){
            return;
        }
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void addressBackPriceSuccess(AddressBackPriceBean.ResultBean addressBackPriceBean,int addressId,String fullAddress,String userInfo) {
        Intent intent = new Intent();
        intent.putExtra("addressId",addressId);
        intent.putExtra("deliveryFee",addressBackPriceBean.getDeliveryFee());
        intent.putExtra("distance",addressBackPriceBean.getDistance());
        intent.putExtra("fullAddress",fullAddress);
        intent.putExtra("userInfo",userInfo);
        setResult(13,intent);
        addressPresenter.modifyDefaultAddress(addressId);
    }

    @Override
    public void addressBackPriceFailed(String msg) {
        if(TextUtils.isEmpty(msg)){
            return;
        }
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void modifyDefaultAddressSuccess(String msg) {
        finish();
    }

    @Override
    public void modifyDefaultAddressFailed(String msg) {
        if(TextUtils.isEmpty(msg)){
            return;
        }
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
}
